package com.schoolhelper.utils;

import org.springframework.stereotype.Component;
import org.testng.annotations.Test;

/**
 * 对称加密，没用MD5是因为想兼容数据库不加密的密码
 * 密钥流加密，直接做一个异或操作
 * 加密结果是byte[]转16进制
 * @author uuz
 * @date 2021/05/14
 */
@Component
public class MySymmetricEncryption {
    static String key = "school_helper"; //密钥


    /**
     * 加密
     *
     * @param content 内容
     * @return {@link String}
     */
    public static String encrypt(String content) {
        byte[] contentBytes = content.getBytes();
        byte[] keyBytes = key.getBytes();

        byte dkey = 0;
        for (byte b : keyBytes) {
            dkey ^= b;
        }
        byte salt = 0;  //随机盐值
        byte[] result = new byte[contentBytes.length];
        for (int i = 0; i < contentBytes.length; i++) {
            salt = (byte) (contentBytes[i] ^ dkey ^ salt);
            result[i] = salt;
        }

        return byte2HexString(result);
    }

    /**
     * 解密
     *
     * @param content 内容
     * @return {@link String}
     */
    public static String decrypt(String content) {
        byte[] contentBytes = hexStringToByteArray(content);
        byte[] keyBytes = key.getBytes();

        byte dkey = 0;
        for (byte b : keyBytes) {
            dkey ^= b;
        }

        byte salt;  //随机盐值
        byte[] result = new byte[contentBytes.length];
        for (int i = contentBytes.length - 1; i >= 0; i--) {
            if (i == 0) {
                salt = 0;
            } else {
                salt = contentBytes[i - 1];
            }
            result[i] = (byte) (contentBytes[i] ^ dkey ^ salt);
        }
        return new String(result);
    }

    /**
     * byte2十六进制字符串
     *
     * @param bytes 字节
     * @return {@link String}
     */
    public static String byte2HexString(byte[] bytes) {
        String hex = "";
        if (bytes != null) {
            for (Byte b : bytes) {
                hex += String.format("%02X", b.intValue() & 0xFF);
            }
        }
        return hex;
    }

    /**
     * 十六进制字符串字节数组
     *
     * @param s str
     * @return {@link byte[]}
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        try {
            for (int i = 0; i < len; i += 2) {
                data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                        + Character.digit(s.charAt(i+1), 16));
            }
        } catch (Exception e) {
             //Log.d("", "Argument(s) for hexStringToByteArray(String s)"+ "was not a hex string");
        }
        return data;
    }
    @Test
    public  void test() {
        String content = "7C037D047C07"; //需要加密的字符

        String result = encrypt(content);

        System.out.println("加密后的值：" + new String(result));
        System.out.println("解密后的值：" + new String(decrypt(new String(result))));
    }
}

